﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class appleader_weight_direct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request["projectid"] != null)
            {
                Session["currentprojectid"] = Request["projectid"].ToString();
                int projectid = Convert.ToInt32(Request["projectid"].ToString());
                int weightmissionid = MasApp.BLL.Search.SearchWeightMissionID(projectid);

                MasApp.Model.weight we = new MasApp.Model.weight();
                we.Weightmissionid = weightmissionid;
                we.Elementtype = "准则";

                foreach (GridViewRow gr in GridView1.Rows)
                {
                    Label lb_cur = (Label)gr.FindControl("lb_cur");
                    Label lb = (Label)gr.FindControl("Label1");
                    we.Elementid = Convert.ToInt32(lb.Text);
                    lb_cur.Text = MasApp.BLL.Search.SearhWeightValue(we).ToString("F4");
                }
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法操作");
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        int projectid = int.Parse(Request["projectid"].ToString());
        //取得总值
        float allValue = 0.0F;
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox tb = (TextBox)gr.Cells[2].FindControl("TextBox1");
            allValue += Convert.ToSingle(tb.Text);
        }
        //设定权重相关信息
        MasApp.Model.weight we = new MasApp.Model.weight();
        we.Weightmissionid = MasApp.BLL.Search.SearchWeightMissionID(projectid);
        we.Elementtype = "准则";
        //更新权值
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox tb = (TextBox)gr.FindControl("TextBox1");
            Label lb = (Label)gr.FindControl("Label1");
            we.Elementid = int.Parse(lb.Text);
            we.Weightvalue = (Convert.ToSingle(tb.Text)) / (allValue);
            //如果已经存在则更新，否则插入
            if (MasApp.DAL.Search.WeightID(we) > 0)
            {
                MasApp.BLL.Update.UpdateWeight(we);
            }
            else
            {
                MasApp.BLL.reg.RegWeight(we);
            }
        }
        MasApp.BLL.ResponseHelper.alert("修改成功","direct.aspx?projectid="+projectid.ToString());
    }
}
